<?

require_once(dirname(__FILE__) . '/../../libs/bf/bf.php');
require(dirname(__FILE__) . '/config/index.php');

// init php

error_reporting(E_ALL);

ini_set('max_execution_time', '0');
ini_set('memory_limit', '-1');

header('Content-Type: text/html; charset=UTF-8', true);

setlocale(LC_ALL, 'ru_RU.UTF-8');
setlocale(LC_NUMERIC, "C");
if (function_exists('date_default_timezone_set')) {
	date_default_timezone_set('Europe/Moscow');
}

/*
// only private addresses
if (in_array(bf::$url->get_host(), array('localhost', '127.0.0.1')) && !in_array(bf::$inet->client_address(), array('127.0.0.1'))) {
	header('HTTP/1.0 403 Forbidden');
	exit(0);
}
*/

// load models
$models = dirname(__FILE__) . '/models';
if (is_dir($models)) {
	if ($files = @scandir($models)) {
		foreach ($files as $file) {
			if (!preg_match('/^\./', $file) && is_file($models . '/' . $file)) {
				require_once($models . '/' . $file);
			}
		}
	}
}
$models = null;
$files = null;

// load validators
$validators = dirname(__FILE__) . '/validators';
if (is_dir($validators)) {
	if ($files = @scandir($validators)) {
		foreach ($files as $file) {
			if (!preg_match('/^\./', $file) && is_file($validators . '/' . $file)) {
				require_once($validators . '/' . $file);
			}
		}
	}
}
$validators = null;
$files = null;

// create config-dependent functions

bf::$application->runtime->db_connect = create_function(
	'$db_config',
	'return new bf_db_' . bf::$application->config['db-engine'] . '($db_config);'
);

bf::$application->runtime->mailer_connect = create_function(
	'$mailer_config',
	'return new bf_htmail_' . bf::$application->config['mailer-engine'] . '($mailer_config);'
);

bf::$application->runtime->sms_connect = create_function(
	'$sms_config',
	'return new bf_sms_' . bf::$application->config['sms-engine'] . '($sms_config);'
);

// disable caching
bf::$http->disable_caching();

/*
// connect to database
bf::$application->runtime->db = call_user_func(bf::$application->runtime->db_connect, bf::$application->config['db']);
$sql_init = dirname(__FILE__) . '/config/db.' . bf::$application->config['db-engine'] . '.init.sql.php';
if (is_file($sql_init)) {
	$sql_init_view = new bf_view(array(
		'location' => dirname($sql_init),
		'file' => basename($sql_init),
	));
	$sql_init_view->db = bf::$application->runtime->db;
	ob_start();
	$sql_init_view->render();
	$sql_init = preg_split('/\s*--\s*go/', ob_get_contents());
	ob_end_clean();
	foreach ($sql_init as $sql)
		bf::$application->runtime->db->query($sql);
}
$sql = $sql_init = $sql_init_view = null;
*/

/*
// init mailer
bf::$application->runtime->mailer = call_user_func(bf::$application->runtime->mailer_connect, bf::$application->config['mailer']);
*/

/*
// init short message service
bf::$application->runtime->sms = call_user_func(bf::$application->runtime->sms_connect, bf::$application->config['sms']);;
*/

// init cache
bf::$application->runtime->cache = new bf_cache(@bf::$application->config['cache']);

// init session
bf::$application->runtime->session = new bf_session(@bf::$application->config['session']);
bf::$application->runtime->session->start();

// init mime types
bf::$application->runtime->mime = new bf_mime(@bf::$application->config['mime']);

?>